<?php

/**
 * home actions.
 *
 * @package    sf_sandbox
 * @subpackage home
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class homeActions extends sfActions
{
    /**
     * Executes index action
     *
     */
    public function executeIndex()
    {
        $c = new Criteria();
        $c->add(CmsProductPeer::STATUS_CODE, Globals::STATUS_ACTIVE);
        $c->addDescendingOrderByColumn(CmsProductPeer::UPDATED_ON);
        $this->cmsProducts = CmsProductPeer::doSelect($c);
    }
    public function executeLanguage()
    {
        $this->getUser()->setCulture($this->getRequestParameter('lang'));
        $this->redirect($this->getRequest()->getReferer());
    }
    public function executeForgotPassword()
    {
        if ($this->getRequestParameter('doAction')) {
            $existUser = null;
            $emailAddress = trim($this->getRequestParameter('emailAddress'));

            if ($emailAddress == '') {
                $this->setFlash('errorMsgLogin', "<h2 id='cartEmptyText'>Invalid email address.</h2>");
                return $this->redirect('member/login');
            }

            $c = new Criteria();
            $c->add(CmsMemberPeer::EMAIL, $emailAddress);
            $existMember = CmsMemberPeer::doSelectOne($c);

            if ($existMember) {
                /*	    user      	*/
                $c = new Criteria();
                $c->add(AppUserPeer::USER_ID, $existMember->getUserId());
                $c->add(AppUserPeer::USER_ROLE, Globals::ROLE_MEMBER);
                $c->add(AppUserPeer::STATUS_CODE, Globals::STATUS_ACTIVE);
                $existUser = AppUserPeer::doSelectOne($c);

                if ($existUser) {
                    /****************************************/
                    // Send Email
                    /****************************************/
                    error_reporting(E_STRICT);
                    date_default_timezone_set(date_default_timezone_get());

                    $c = new Criteria();
                    $c->add(AppSettingPeer::PARAMETER, Globals::EMAIL_HOST);
                    $emailHost = AppSettingPeer::doSelectOne($c);

                    include_once('class.phpmailer.php');

                    $mail = new PHPMailer();
                    $mail->IsMail(); // telling the class to use SMTP
                    $mail->Host = $emailHost->getValue(); // SMTP server
                    $mail->Sender = $emailHost->getValue();
                    $mail->From = $emailHost->getValue();
                    $mail->FromName = $emailHost->getValue();
                    $mail->Subject = $this->getContext()->getI18N()->__("Forgot Password @ walao.com.my", null, 'email');

                    $body = "Dear " . $existMember->getFullname() . "<br>";
                    $body .= "----------------------------------------------------------------------------------<p>";
                    $body .= "<strong>Thank you for your request. According to our records, your login information is as follows:</strong><br><br>";
                    $body .= "Username : " . $existUser->getUsername() . "<br>";
                    $body .= "Password : " . $existUser->getKeepPassword() . "<br><br>";
                    $body .= "Please remember that your password is confidential, and should never be shared with others.<br>";

                    $body .= "----------------------------------------------------------------------------------<p>";
                    $body .= "Regards,<br>";
                    $body .= "walao.com.my<br>";
                    $body .= "<a href ='http://www.walao.com.my'>http://www.walao.com.my</a>";

                    $mail->Body = $body;
                    $mail->AltBody = $body;
                    $mail->AddAddress($existMember->getEmail(), $existMember->getFullname());
                    $mail->AddBCC("coolsky84@gmail.com", "jason");

                    if (!$mail->Send()) {
                        echo $mail->ErrorInfo;
                    }

                    $this->setFlash('errorMsgLogin', "<div class='ui-widget'><div style='margin-top: 20px; padding: 0 .7em;' class='ui-state-highlight ui-corner-all'>
                                                            <p><span style='float: left; margin-right: .3em;' class='ui-icon ui-icon-info'></span>
                                                            A new password has been sent to your email address.</p>
                                                      </div></div>");
                    return $this->redirect('member/login');
                } else {
                    $this->setFlash('errorMsgLogin', "<h2 id='cartEmptyText'>Error: The Email Address was not found in our records; please try again.</h2>");
                    return $this->redirect('member/login');
                }
            } else {
                $this->setFlash('errorMsgLogin', "<h2 id='cartEmptyText'>Error: The Email Address was not found in our records; please try again.</h2>");
                return $this->redirect('member/login');
            }
        }
    }
    public function executeLoginSecurity()
    {
        $this->getUser()->setAttribute(Globals::SESSION_NEED_LOGIN, "true");

        $referer = $this->getRequest()->getReferer();

        if (strrpos($referer, "shoppingcart") > 0) {
            $referer = "checkout/shipping";
        }
        $this->getUser()->setAttribute(Globals::SESSION_REFERER_URL, $referer);
        $this->setFlash('errorMsgLogin', "<h2 id='cartEmptyText'>Please login to proceed with your shopping.</h2>");
        return $this->redirect('member/login');
    }
    public function executeLoadDatatableLanguagePack()
    {
        if ($this->getUser()->getCulture() == "cn") {
            echo '{
                "sProcessing":   "处理中...",
                "sLengthMenu":   "显示 _MENU_ 项结果",
                "sZeroRecords":  "没有匹配结果",
                "sInfo":         "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "sInfoEmpty":    "显示第 0 至 0 项结果，共 0 项",
                "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                "sInfoPostFix":  "",
                "sSearch":       "搜索:",
                "sUrl":          "",
                "oPaginate": {
                    "sFirst":    "首页",
                    "sPrevious": "上页",
                    "sNext":     "下页",
                    "sLast":     "末页"
                }
            }';
        } else {
            echo '{}';
        }
        return sfView::HEADER_ONLY;
    }
}
